package com.baizhi.service.impl;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.DeleteCacheAnnotation;
import com.baizhi.dao.UserDao;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.util.ThreadPollUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;


@Service
public class UserServiceImpl implements UserService {

    private Logger logger= LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserDao userDao;



    @Override
    @AddCacheAnnotation
    public Map selectAllUser(int page, int limit, String userName) {
        if(userName!=null&&!userName.equals("")){
            userName="%"+userName+"%";
        }else{
            userName=null;
        }
        int offset=(page-1)*limit;
        List<User> list=userDao.selectAllUser(offset,limit,userName) ;
        int count=userDao.selectCount(userName);
        Map map=new HashMap();
        map.put("data",list);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public Map registerUser(User user) {
        int count=userDao.insertUser(user);
        Map map=new HashMap();
        if(count>0){
            map.put("isAdd",true);
        }else{
            map.put("isAdd",false);
        }

        return map;
    }

    @Override
    @AddCacheAnnotation
    public boolean loginUser(String userName, String userPassword) {
        User user=userDao.selectOneUser(userName);
        if(userName!=null&&user.getUserPassword().equals(userPassword)){
            return true;
        }else{
            return false;
        }

    }

    @Override
    @AddCacheAnnotation
    public List<Map> selectBySex() {
        return userDao.selectBySex();
    }

    @Override
    @AddCacheAnnotation
    public List<Map> selectByProvince() {
        return userDao.selectByProvince();
    }

    @Override
    @AddCacheAnnotation
    public Map selectByDayCount() {

        Map map=new HashMap();
        map.put("one",userDao.selectByDayCount(7,1));
        map.put("two",userDao.selectByDayCount(14,7));
        map.put("three",userDao.selectByDayCount(21,14));
        return map;
    }

    @Override
    @AddCacheAnnotation
    public Map selectByThread() throws InterruptedException {
        Map map = new HashMap();
//        1.创建三个线程分别执行三个任务 创建计数器
        CountDownLatch countDownLatch = new CountDownLatch(3);
        System.out.println("创建三个线程");
        logger.debug("创建三个线程");
        logger.info("创建三个线程");
        logger.warn("创建三个线程");
        logger.error("创建三个线程");
        ThreadPoolExecutor executor = ThreadPollUtil.executor;

        executor.submit(new Runnable() {
            @Override
            public void run() {
//                注册量
                Map map1 = selectByDayCount();
                map.put("count",map1);
                System.out.println("注册量");
//                计数器减一
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                男女人数
                List<Map> maps = selectBySex();
                map.put("sex",maps);
                //                计数器减一
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                地区分布
                List<Map> maps = selectByProvince();
                map.put("province",maps);
                //                计数器减一
                countDownLatch.countDown();
            }
        });


//        2.让主线程等待子线程执行结束
        countDownLatch.await();

        return map;
    }

}
